﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ApplLogic.ApplClasses;
using TCPClient.UI.LogicForUserControls;

namespace TCPClient.UI.UserControls
{
    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
    public partial class WeekTableManagerUserControl : UserControl
    {
        private List<DayInAWeekManagerUserControl> m_Days = new List<DayInAWeekManagerUserControl>(7);
        private DateTime m_StartOfWeek;
        private DateTime m_EndOfWeek;

        public WeekTableManagerUserControl()
        {
            InitializeComponent();
        }

        private void setDaysToList()
        {
            dayInAWeekManagerUserControlSunday.DayOfWeek = DayOfWeek.Sunday;
            m_Days.Add(dayInAWeekManagerUserControlSunday);
            dayInAWeekManagerUserControlMonday.DayOfWeek = DayOfWeek.Monday;
            m_Days.Add(dayInAWeekManagerUserControlMonday);
            dayInAWeekManagerUserControlTuesday.DayOfWeek = DayOfWeek.Tuesday;
            m_Days.Add(dayInAWeekManagerUserControlTuesday);
            dayInAWeekManagerUserControlWednesday.DayOfWeek = DayOfWeek.Wednesday;
            m_Days.Add(dayInAWeekManagerUserControlWednesday);
            dayInAWeekManagerUserControlThursday.DayOfWeek = DayOfWeek.Thursday;
            m_Days.Add(dayInAWeekManagerUserControlThursday);
            dayInAWeekManagerUserControlFriday.DayOfWeek = DayOfWeek.Friday;
            m_Days.Add(dayInAWeekManagerUserControlFriday);
            dayInAWeekManagerUserControlSaturday.DayOfWeek = DayOfWeek.Saturday;
            m_Days.Add(dayInAWeekManagerUserControlSaturday);
        }

        public void SetWeekData(DateTime startOfWeek, DateTime endOfWeek)
        {
            if (m_Days.Count == 0)
            {
                setDaysToList();
                setLocationsOfAllShiftsInDays();
                setLabelsOfShiftLenght();
            }
            m_StartOfWeek = startOfWeek;
            m_EndOfWeek = endOfWeek;
            initializeHoursOfAllWeekDays();
        }

        private void initializeHoursOfAllWeekDays()
        {
            int numOfHoursToAdd = 0;
            int labelIndex = 0;
            foreach (var dayInAweek in m_Days)
            {
                dayInAweek.Date = m_StartOfWeek.AddHours(numOfHoursToAdd);
                numOfHoursToAdd += 24;
                dayInAweek.SetTimesOfShiftCells();
                this.daysLabelUserControlManager1.SetLabelOfText(labelIndex, dayInAweek.DayOfWeek.ToString() +
                                "\n" + dayInAweek.Date.ToShortDateString());
                labelIndex++;
            }
        }

        private void setLocationsOfAllShiftsInDays()
        {
            foreach (var dayUserControl in m_Days)
            {
                dayUserControl.AddControlsOfShifts();
            }
        }

        public DateTime StartOfWeek
        {
            get { return m_StartOfWeek; }
            set { m_StartOfWeek = value; }
        }

        public DateTime EndOfWeek
        {
            get { return m_EndOfWeek; }
            set { m_EndOfWeek = value; }
        }

        private void setLabelsOfShiftLenght()
        {
            for (int i = 0; i < DefaultConfigurationHandler.Instance.NumberOfShiftsPerDay; i++)
            {
                shiftTimesContainerUserControl1.AddSingleShiftTimeCell(m_StartOfWeek, i);
            }
        }

        public void DrawRequestedWorkers(DateTime startTime, DateTime endTime, List<ApplSingleRequest> workers)
        {
            foreach (var dayInAWeek in m_Days)
            {
                dayInAWeek.DrawRequestedWorkers(startTime, endTime, workers);
            }
        }

        public void SetCheckBoxToListenToToAllWorkers(CheckBox checkBox1, CheckBox checkBox2)
        {
            foreach (var dayUc in m_Days)
            {
                dayUc.SetCheckBoxToListenToToAllWorkers(checkBox1, checkBox2);
            }
        }

        public void DrawRegisteredWorkers(DateTime startTime, DateTime endTime, List<ApplSingleResult> workersResult, bool isToClean = false)
        {
            foreach (var dayInAWeek in m_Days)
            {
                dayInAWeek.DrawRegisteredWorkers(startTime, endTime, workersResult, isToClean);
            }
        }

        public void CleanAll()
        {
            foreach (var dayInAWeek in m_Days)
            {
                dayInAWeek.CleanAll();
            }
        }

        public void RegisterToClickEventsOfTheSingleWorkersLabelUserControls(EventHandler handler)
        {
            foreach (var dayInAWeek in m_Days)
            {
                dayInAWeek.RegisterToClickEventsOfTheSingleWorkersLabelUserControls(handler);
            }
        }

        public void RegisterToClickEventsOfTheSingleWorkersUserControls(EventHandler handler)
        {
            foreach (var dayInAWeek in m_Days)
            {
                dayInAWeek.RegisterToClickEventsOfTheSingleWorkersUserControls(handler);
            }
        }
    }
}
